package com.tianzmp.ttq.admin.controller;

import com.tianzmp.ttq.admin.common.BaseResult;
import com.tianzmp.ttq.admin.common.Result;
import com.tianzmp.ttq.admin.controller.reqDto.UserInfoPageList;
import com.tianzmp.ttq.admin.controller.reqDto.UserInfoUpdateDTO;
import com.tianzmp.ttq.admin.controller.reqDto.UserMenusDTO;
import com.tianzmp.ttq.admin.controller.reqDto.UserRoleSaveDTO;
import com.tianzmp.ttq.admin.controller.resDto.MenuTree;
import com.tianzmp.ttq.admin.controller.resDto.UserInfoPageListResponse;
import com.tianzmp.ttq.admin.controller.resDto.ResponseUserListDto;
import com.tianzmp.ttq.admin.controller.resDto.UserInfoGetById;
import com.tianzmp.ttq.admin.entity.PageInfo;
import com.tianzmp.ttq.admin.entity.UserInfo;
import com.tianzmp.ttq.admin.service.IUserInfoService;
import com.tianzmp.ttq.admin.util.MapperUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.RolesAllowed;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 用户基本数据表 前端控制器
 * </p>
 *
 * @author ttq
 * @since 2022-07-26
 */
@RestController
@RequestMapping("/admin/userInfo")
@Api(value = "user manage controller")
@Log4j2
public class UserInfoController {

    @Resource
    private IUserInfoService userInfoService;


    /**
     * <pre>
     *     this is a user list
     * </pre>
     * @return
     */
    @ApiOperation(value = "does pageInfo user",notes = "不分页查询所有用户",httpMethod = "GET")
    @GetMapping("/list")
    @ApiResponses(value = {
            @ApiResponse(code = 200,message = BaseResult.SUCCESS_MSG,response = ResponseUserListDto.class)
    })
    public BaseResult<List<ResponseUserListDto>> list(){
        List<UserInfo> list = userInfoService.list();
        List<ResponseUserListDto> respList = MapperUtils.mapAsList(ResponseUserListDto.class, list);
        return Result.ok(respList);
    }

    /**
     * <pre>
     *     user list is a pageInfo
     * </pre>
     * @param dto  当前第几页
     * @return
     */
    @ApiOperation(value = "pageInfo user",notes = "分页查询用户信息",httpMethod = "POST")
    @PostMapping(value = "/pageList")
    public BaseResult<PageInfo<List<UserInfoPageListResponse>>> pageList(@RequestBody UserInfoPageList dto){
        PageInfo<List<UserInfoPageListResponse>> listPageInfo = userInfoService.pageList(dto);
        return Result.ok(listPageInfo);
    }


    /**
     * <pre>
     *
     * </pre>
     * @param id user-id
     * @return
     */
    @ApiOperation(value = "user detail",notes = "根据用户id查询用户详情",httpMethod = "GET")
    @GetMapping(value = "/getById/{id}")
    @RolesAllowed("role:user:view")
    public BaseResult<UserInfoGetById> getById(@PathVariable(value = "id")Long id){
        UserInfoGetById bean = userInfoService.getById(id);
        return Result.ok(bean);
    }

    @ApiOperation(value = "update user",notes = "更具用户id更新用户",httpMethod = "POST")
    @PostMapping(value = "/update")
    public BaseResult<Boolean> update(@Valid @RequestBody UserInfoUpdateDTO dto){
        boolean result = userInfoService.update(dto);
        return Result.ok(result);
    }

    @ApiOperation(value = "delete users",notes = "根据用户id删除用户",httpMethod = "DELETE")
    @DeleteMapping(value = "/delete/{userId}",produces = {"application/json"})
    public BaseResult<Boolean> delete(@PathVariable(value = "userId") List<Long> userId){
        boolean result = userInfoService.delete(userId);
        return Result.ok(result);
    }

    @ApiOperation(value = "user bind role",notes = "用户分配角色",httpMethod = "POST")
    @PostMapping(value = "/userAuthorize")
    public BaseResult<Boolean> userAuthorize(@RequestBody UserRoleSaveDTO dto){
        boolean result = userInfoService.userAuthorize(dto.getUserId(),dto.getRoleIds());
        return Result.ok(result);
    }



    @ApiOperation(value = "user menu",notes = "用户拥有哪些菜单",httpMethod = "POST")
    @PostMapping(value = "/getMenuIds")
    public BaseResult<List<MenuTree>> userAuthorize(@RequestBody UserMenusDTO dto){
        List<MenuTree> menuTree = userInfoService.getMenuTree(dto);
        return Result.ok(menuTree);
    }

}
